<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>连接添加页面</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="{{domain}}/resource/admin/mini/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="{{domain}}/resource/admin/mini/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>

<body>

    <div class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label required">数据库名称</label>
            <div class="layui-input-block">
                <input type="text" id="dbname" name="dbname" lay-verify="required" lay-reqtext="数据库名不能为空"
                    placeholder="请输入新的数据库名" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">数据库类型</label>
            <div class="layui-input-block">
                <select name="dbtype" id="dbtype" lay-filter="dbtype" lay-verify="required">
                    <option value="mysql">MySQL</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">连接地址</label>
            <div class="layui-input-block">
                <input type="text" name="host" id="host" lay-verify="required" lay-reqtext="数据库地址不能为空"
                    placeholder="数据库连接地址" value="localhost" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">连接端口</label>
            <div class="layui-input-block">
                <input type="number" name="port" id="port" lay-verify="required" lay-reqtext="连接端口不能为空"
                    placeholder="数据库连接端口" value="3306" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">连接用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" id="username" lay-verify="required" lay-reqtext="连接用户名不能为空"
                    placeholder="数据库连接用户名" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">连接密码</label>
            <div class="layui-input-block">
                <input type="text" name="password" id="password" lay-verify="required" lay-reqtext="连接密码不能为空"
                    placeholder="数据库连接密码" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">字符集</label>
            <div class="layui-input-block">
                <input type="text" name="charset" id="charset" placeholder="数据库字符集" value="utf8" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="saveBtn">确认保存</button>
                <button class="layui-btn layui-btn-normal" id="checkBtn" lay-filter="checkBtn">连接检测</button>
            </div>
        </div>
    </div>
    <script src="{{domain}}/resource/admin/mini/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
    <script>
        var connectCheck = false;
        layui.use(['form'], function () {
            var form = layui.form,
                layer = layui.layer,
                $ = layui.$;


            //监听提交
            form.on('submit(saveBtn)', function (data) {
                if (!connectCheck) {
                    layer.msg('请先通过连接检测！');
                }

                $.post('{{domain}}/admin/api/dbs/newdb', {
                    host: data.field.host,
                    port: data.field.port,
                    dbname: data.field.dbname,
                    dbtype: data.field.dbtype,
                    username: data.field.username,
                    password: data.field.password,
                    charset: data.field.charset,
                    status: 1
                }, function (res) {
                    if (res.msg != '') {
                        layer.msg(res.msg + '...');
                    } else if (res.data == 'OK') {
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                        parent.window.location.reload();
                    } else {
                        layer.msg('未知的程序错误...');
                    }
                }).fail(function (res) {
                    layer.msg('操作异常：' + res.responseJSON.msg + '！');
                });

            });

            // 检测连接程序
            $('#checkBtn').click(function () {
                $.post('{{domain}}/admin/api/dbs/check?range=external', {
                    host: $('#host').val(),
                    port: $('#port').val(),
                    dbname: $('#dbname').val(),
                    dbtype: $('#dbtype').val(),
                    username: $('#username').val(),
                    password: $('#password').val(),
                    charset: $('#charset').val()
                }, function (res) {
                    if (res.data == 'OK') {
                        connectCheck = true;
                        layer.msg('连接检测已通过！');
                    }
                }).fail(function (res) {
                    layer.msg('连接错误：' + res.responseJSON.msg + '！');
                });
            });

        });
    </script>
</body>

</html>